'use strict';

const Controller = require('egg').Controller;
const jwt = require("jsonwebtoken")


class HomeController extends Controller {
    async login() {
        const { ctx, app } = this;
        const { username, password } = ctx.request.body;
        const data = await app.mysql.get('login', { username, password })
        const token = jwt.sign(data.username, '1901B')
        if (data) {
            ctx.body = {
                code: "000",
                msg: "登录成功",
                token,
                data
            }
        } else {
            ctx.body = {
                code: "001",
                msg: "账号或密码错误",
                token
            }
        }
    }
    async regis() {
        const { ctx, app } = this;
        const { username, password } = ctx.request.body;
        const data = await app.mysql.get('login', { username })
        if (data) {
            ctx.body = {
                code: "000",
                msg: "账号已存在",
                data
            }
        } else {
            await app.mysql.insert('login', { username, password })
            ctx.body = {
                code: "001",
                msg: "注册成功",
                data
            }
        }
    }
}

module.exports = HomeController;
